﻿using System;
using System.Collections.Generic;
using System.Text;

namespace DevCore.UI.Web.Grid
{
    /// <summary>
    /// EasyGrid 辅助类
    /// </summary>
    public class EasyGridHelper
    {
        public String Name { get; set; }
        public String Height { get; set; }
        public String Width { get; set; }
        public int PageSize { get; set; }
        public String DataUrl { get; set; }
        public Boolean HasPage { get; set; }
        public String Title { get; set; }
        public String LoadMsg { get; set; }
        public List<GridColumnInfo> GridColumnInfos { get; set; }
        

        public EasyGridHelper()
        {
            Name = "myGrid";
            Height = "500";
            Width = "100%";
            PageSize = 20;
            LoadMsg = "正在加载...,请稍候.";
        }

        public EasyGridHelper(String name, String width, String height)
        {
            Name = name;
            Width = width;
            Height = height;
        }

        /// <summary>
        /// 设置Grid信息
        /// </summary>
        public String SetGrid()
        {
            String isPage = "";
            isPage = HasPage ? "true" : "false";

            StringBuilder sbGrid = new StringBuilder();
            sbGrid.Append(" $('#"+Name+"').datagrid({\r\n");
            sbGrid.Append("     title: '" + Title + "',\r\n");
            sbGrid.Append("     width: \"" + Width + "\",\r\n");
            sbGrid.Append("     height: " + Height + ",\r\n");
            sbGrid.Append("     pageSize: " + PageSize + ",\r\n");
            sbGrid.Append("     loadMsg: '" + LoadMsg + "',\r\n");
            //判断是否有Columns
            if(HasColumns())
            {
                sbGrid.Append(GetGridColumns() +"\r\n");
            }
            sbGrid.Append("     url: '" + DataUrl + "',\r\n");
            sbGrid.Append("     singleSelect: true,\r\n");
            sbGrid.Append("     pagination: " + isPage + "\r\n");
            sbGrid.Append(" });\r\n");

            return sbGrid.ToString();

        }

        /// <summary>
        /// 设置当前GridColumn的值
        /// </summary>
        /// <returns></returns>
        public String GetGridColumns()
        {
            StringBuilder sbColumns = new StringBuilder();
            String strColumnInfo = "";
            sbColumns.Append("columns:[[\r\n");

            for (int i = 0; i < GridColumnInfos.Count; i++ )
            {
                GridColumnInfo columnInfo = GridColumnInfos[i];
                strColumnInfo = "field:'" + columnInfo.FieldName + "'," + "title:'"+columnInfo.FieldCaption+"'";
                if(columnInfo.IsCheckBox)
                {
                    strColumnInfo += ", checkbox:true";
                }
                else
                {
                    strColumnInfo += ", width:" + columnInfo.Width;
                }
                if( i == GridColumnInfos.Count - 1)
                {
                    sbColumns.Append("{" + strColumnInfo + "}");
                }
                else
                {
                    sbColumns.Append("{" + strColumnInfo + "},\r\n");
                }
            }
            sbColumns.Append("]],");
            return sbColumns.ToString();
        }

        /// <summary>
        /// 判断当前的Grid是否有列。
        /// </summary>
        /// <returns></returns>
        public Boolean HasColumns()
        {
            if(GridColumnInfos == null || GridColumnInfos.Count < 1)
            {
                return false; 
            }
            else
            {
                return true;
            }
        }

        

    }

    /// <summary>
    /// 列属性
    /// </summary>
    public class GridColumnInfo
    {
        public String FieldName { get; set; }
        public String FieldCaption { get; set; }
        public Boolean IsCheckBox { get; set; }
        public String Width { get; set; }

    }
}